package com.example.yuntiku.dao.impl;

import com.example.yuntiku.dao.BaseDao;
import com.example.yuntiku.entity.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UsersDao extends BaseDao {
    //    SELECT `uid`,`uname`,`upwd`,`email`,`cid`,`houseid` FROM `users`
    public User selectUsers(String email, String upwd) {
        Connection conn=null;
        PreparedStatement stmt=null;
        ResultSet rs=null;
        try {
            conn=super.getConnection();
            stmt=conn.prepareStatement("SELECT `uid`,`uname`,`upwd`,`email`,`cid`,`houseid` FROM `users` WHERE `email`=? AND `upwd`=?");
            stmt.setString(1,email);
            stmt.setString(2,upwd);
            rs=stmt.executeQuery();
            while (rs.next()) {
                User entity = new User();
                entity.setUid(rs.getInt("uid"));
                entity.setUname(rs.getString("uname"));
                entity.setUpwd(rs.getString("upwd"));
                entity.setEmail(rs.getString("email"));
                entity.setCid(rs.getInt("cid"));
                entity.setHouseid(rs.getInt("houseid"));
                return entity;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            super.closeAll(conn,stmt,rs);
        }
        return null;
    }

    public int insert(User users){
        String sql="INSERT INTO `yuntiku`.`users` (`email`,`uname`,`upwd`,cid) VALUES(?,?,?,1) ;";
        return super.executeUpdate(sql,
                users.getEmail(),
                users.getUname(),
                users.getUpwd());
    }
    public int update(User users){
        String sql="UPDATE `yuntiku`.`users` SET `upwd` = ? WHERE `email` = ?;";
        return super.executeUpdate(sql,
                users.getUpwd(),
                users.getEmail());
    }
    public boolean isEmailRegistered(String email) {
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            conn = super.getConnection();
            stmt = conn.prepareStatement("SELECT COUNT(*) FROM `users` WHERE `email` = ?");
            stmt.setString(1, email);
            rs = stmt.executeQuery();
            if (rs.next()) {
                int count = rs.getInt(1);
                return count > 0;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            super.closeAll(conn, stmt, rs);
        }
        return false;
    }

    public User selectById(int id){
        User users =null;
        Connection conn=null;
        PreparedStatement stmt=null;
        ResultSet rs=null;
        try {
            conn=super.getConnection();
            stmt=conn.prepareStatement("SELECT `uid`,`uname`,`upwd`,`email` FROM `users` WHERE `uid`=?");
            stmt.setInt(1,id);
            rs=stmt.executeQuery();
            while (rs.next()) {
                User entity=new User();
                entity.setUid(rs.getInt("uid"));
                entity.setUname(rs.getString("uname"));
                entity.setUpwd(rs.getString("upwd"));
                entity.setEmail(rs.getString("email"));
                entity.setUid(rs.getInt("uid"));
                return entity;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            super.closeAll(conn,stmt,rs);
        }
        return users;
    }

}
